/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年1月27日 上午11:50:15
 */
package com.lenovo.lmrp.server.assigntracer.events;

import java.util.Objects;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.lenovo.lmrp.api.model.entity.TicketEventPO;
import com.lenovo.lmrp.base.event.EventContent;
import com.lenovo.lmrp.server.assigntracer.domain.ticket.ITicketRepository;
import com.lenovo.lmrp.server.assigntracer.domain.ticket.TicketBO;
import com.lenovo.lmrp.server.assigntracer.domain.workflow.IProcessService;
import com.lenovo.lmrp.server.assigntracer.domain.workflow.ProcessSelector;
import com.lenovo.sdc.framework.mq.IMQReceiver;
import com.lenovo.sdc.framework.util.JSONHelper;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * @author songyz
 *
 */
public class ItsmGatewayHandle implements IMQReceiver {
    private static final Logger logger = LoggerFactory.getLogger(ItsmGatewayHandle.class);

    private ITicketRepository ticketRepository;

    private ProcessSelector processSelector;
    
    private IProcessService processService;
    
    @Inject
    public void set(ITicketRepository ticketRepository) {
        this.ticketRepository = ticketRepository;
    }
    
    @Inject
    public void set(ProcessSelector processSelector) {
        this.processSelector = processSelector;
    }

    @Inject
    public void set(IProcessService processService) {
        this.processService = processService;
    }
    
    @Override
    public void onMessage(String topic, Object content) {

        EventContent<TicketEventPO> event = null;

        try {
            event = JSONHelper.fromJSON((String) content, new TypeReference<EventContent<TicketEventPO>>() {});
        }
        catch (Exception e) {
            throw new TraceableException(ErrorCode.COMM_PARSE_FAILED, e);
        }
        
        switch (event.getCode()) {
            case TICKET_CREATE: {

                TicketBO ticketBO = ticketRepository.createTicketBO(event.getContent().getItsmCode(), event.getContent().getTicketID(), true);
                if (Objects.isNull(ticketBO)) {
                    logger.warn("not found ticket{}-{}", event.getContent().getItsmCode(), event.getContent().getTicketID());
                    return;
                }
                ticketBO.startTicketProcess(processSelector, processService);
                break;
            }
            case TICKET_UPDATE:
            	logger.info("update ticket {} do {}",event.getContent().getTicketID(), event.getContent().getAction());
            	TicketBO ticketBO = ticketRepository.createTicketBO(event.getContent().getItsmCode(), event.getContent().getTicketID(), true);
            	ticketBO.notifyClient(event.getContent().getTicketID());
                break;
            default:
                logger.warn("ignore event code {}", event.getCode().getCode());
        }
    }
}
